package org.bonorsoft.modules.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.constant.CommonConstant;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.modules.hospital.entity.*;
import org.bonorsoft.modules.hospital.mapper.*;
import org.bonorsoft.modules.hospital.service.ITotalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @Description: 统计
 * @Author: WilliamYe
 * @Date:   2020-10-14
 * @Version: V1.0
 */
@Service
public class TotalServiceImpl implements ITotalService {

    @Resource
    private TotalMapper totalMapper;

    @Resource
    private AdmitteMapper admitteMapper;

    @Resource
    private RecordReferralMapper recordReferralMapper;

    @Resource
    private CommonReferralMapper commonReferralMapper;

    @Resource
    private InviteExpertMapper inviteExpertMapper;

    @Resource ZzyjsViewMapper zzyjsViewMapper;

    @Override
    public Result<JSONObject> queryIndex() {
        Result<JSONObject> result = new Result<JSONObject>();
        //今日门诊量
        Integer todayOutpatient = totalMapper.getTodayOutpatient();
        //今日收入院
        Map<String,String> today = DateUtils.getToday();
        Integer todayAdmitte = getTodayAdmitte(today.get("start_time"),today.get("end_time"));
        //今日医保转出人次
        Integer todayRecord = getTodayRecord(today.get("start_time").substring(0,today.get("start_time").indexOf(" ")));
        //今日急诊转出人数
        Integer todayCommon = getTodayCommon(today.get("start_time").substring(0,today.get("start_time").indexOf(" ")));

        JSONObject obj = new JSONObject();
        obj.put("todayOutpatient", todayOutpatient);
        obj.put("todayAdmitte", todayAdmitte);
        obj.put("todayRecord", todayRecord);
        obj.put("todayCommon", todayCommon);
        result.setResult(obj);
        result.success("查询成功");
        return result;
    }

    /**
     * 医保转诊
     * @return
     */
    @Override
    public List<TotalEntity> getRecordData() {
        List<TotalEntity> recordList = totalMapper.getRecordData(getYear());
        return recordList;
    }

    /**
     * 普通转诊
     * @return
     */
    @Override
    public List<TotalEntity> getCommonData() {
        List<TotalEntity> commonList = totalMapper.getCommonData(getYear());
        return commonList;
    }

    @Override
    public List<AnalysisEntity> getAnalysisData() {
        List<AnalysisEntity> list = new ArrayList<>();
        //东营市人民医院
        Integer dongyingNum = getHospitalNum(CommonConstant.HOSPITAL_DONGYING);
        AnalysisEntity analysis_dongying = new AnalysisEntity();
        analysis_dongying.setName(CommonConstant.HOSPITAL_DONGYING);
        analysis_dongying.setValue(dongyingNum);
        list.add(analysis_dongying);

        //胜利油田中心医院
        Integer youtianNum = getHospitalNum(CommonConstant.HOSPITAL_YOUTIAN);
        AnalysisEntity analysis_youtian = new AnalysisEntity();
        analysis_youtian.setName(CommonConstant.HOSPITAL_YOUTIAN);
        analysis_youtian.setValue(youtianNum);
        list.add(analysis_youtian);

        //其他医院
        Integer qitaNum = getHospitalNum(CommonConstant.HOSPITAL_QITA);
        AnalysisEntity analysis_qita = new AnalysisEntity();
        analysis_qita.setName(CommonConstant.HOSPITAL_QITA);
        analysis_qita.setValue(qitaNum);
        list.add(analysis_qita);


        return list;


    }

    private Integer getHospitalNum(String hospital) {
        QueryWrapper<CommonReferral> queryWrapper = new QueryWrapper<>();
        if(!CommonConstant.HOSPITAL_QITA.equals(hospital)){
            queryWrapper.eq("roll_out_hosp",hospital);
        }else{
            queryWrapper.ne("roll_out_hosp",CommonConstant.HOSPITAL_DONGYING)
                    .ne("roll_out_hosp",CommonConstant.HOSPITAL_YOUTIAN);
        }
        return commonReferralMapper.selectCount(queryWrapper);
    }

    @Override
    public Result<JSONObject> getHospitalAnalysis() {
        Result<JSONObject> result = new Result<>();
        //获取今年收入院每月数据
        List<TotalEntity> admitteNow = admitteMapper.getAdmitteYearData(getYear());
        //获取去年收入院每月数据
        List<TotalEntity> admittePrev = admitteMapper.getAdmitteYearData(getYear()-1);
        JSONObject json = new JSONObject();
        json.put("now",admitteNow);
        json.put("prev",admittePrev);
        result.setResult(json);
        result.success("查询成功！");
        return result;

    }

    /**
     * 外请专家人次
     * @return
     */
    @Override
    public List<TotalEntity> getOutsideData() {
        List<TotalEntity> outsideData = inviteExpertMapper.getOutsideData(getYear());
        return outsideData;
    }

    @Override
    public List<TotalEntity> getSuperiorData(int year, int type) {
        return totalMapper.getSuperiorData(year,type);
    }

    /**
     * 今日收入院-对接HIS数据库
     * @param start_time
     * @param end_time
     * @return
     */
    private Integer getTodayAdmitte(String start_time, String end_time) {
        QueryWrapper<ZzyjsView> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("pat_in_time", start_time, end_time);
        Integer todayAdmitte = zzyjsViewMapper.selectCount(queryWrapper);
        return todayAdmitte;
    }

    /**
     * 今日医保转出人次
     * @param time
     * @return
     */
    private Integer getTodayRecord(String time) {
        QueryWrapper<RecordReferral> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roll_out_time", time);
        return recordReferralMapper.selectCount(queryWrapper);
    }

    /**
     *今日急诊转出人数
     * @param time
     * @return
     */
    private Integer getTodayCommon(String time) {
        QueryWrapper<CommonReferral> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roll_out_time", time);
        return commonReferralMapper.selectCount(queryWrapper);
    }

    private Integer getYear(){
        return DateUtils.getYear();
    }

}
